package com.zm.demo.sc.mybatisplus.main;

import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.TemplateConfig;
import com.baomidou.mybatisplus.generator.config.converts.MySqlTypeConvert;
import com.baomidou.mybatisplus.generator.config.po.TableField;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import com.baomidou.mybatisplus.generator.config.rules.IColumnType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

/**
 * @desc:
 * @author: z01140
 * @date: 2021/7/2 14:51
 */
public class GenerateCodeMain {

    public static void main(String[] args) {
//        generator(true, "customer_info","loan_trans_info","loan_trans_info_ext","customer_repay_plan","capital_repay_plan","capital_inner_repay_plan","repay_plan_sync","fgp_repay_plan","fgp_repay_plan_snapshot","fgp_repay_plan_ext","repayment_recorded_notify_customer","repayment_recorded_notify_capital","repayment_recorded_detail_customer","repayment_recorded_detail_customer_ext","repayment_recorded_detail_capital","clear_settlement_detail","compensation_detail","overdue_fee_sync_process","overdue_fee_sync_notify","guarantee_info_config","guarantee_info_config_snapshot");
        generator(false, "loan_trans_info");
    }

    private static void generator(boolean isDTO, String ... tableName){
        DataSourceConfig dataSourceConfig = new DataSourceConfig();
        dataSourceConfig.setUrl("jdbc:mysql://xxx.168.27.xxx:3306/fgp")
//                .setDriverName("com.mysql.jdbc.Driver")
                .setDriverName("com.mysql.jdbc.Driver")
                .setUsername("root")
                .setPassword("xxx")
                .setSchemaName("fgp")
                .setTypeConvert(new MySqlTypeConvert(){
                    @Override
                    public IColumnType processTypeConvert(GlobalConfig globalConfig, TableField tableField) {
                        if (tableField.getType().toLowerCase().contains("tinyint")){
                            return DbColumnType.INTEGER;
                        }
                        return super.processTypeConvert(globalConfig, tableField);
                    }
                })
        ;

        String projectPath = System.getProperty("user.dir");
//        projectPath = projectPath + "\\nct-core-common\\src\\main\\java\\";
//        projectPath = "C:\\Users\\z01140\\IdeaProjects\\nct\\nct-repay\\nct-repay-core\\src\\main\\java\\";
        projectPath = "D:\\zm\\工作\\代码生成";
        System.out.println(projectPath);
        GlobalConfig globalConfig = new GlobalConfig()
                .setFileOverride(true)
                .setOpen(true)
                .setOutputDir(projectPath)
                .setAuthor("01140")
                .setDateType(DateType.ONLY_DATE)
                .setBaseColumnList(false)
                .setBaseResultMap(true)
                .setServiceName("%sService")
                .setEntityName(isDTO ? "%sDTO" : "%s")
                .setMapperName("%sMapper")
                .setXmlName("%sMapper")
                .setServiceImplName("%sServiceImpl")

                ;

//        PackageConfig packageConfig = new PackageConfig().setParent("com.smy.nct.repay")
        PackageConfig packageConfig = new PackageConfig().setParent("com.smy.fgp.core")
//                .setModuleName("repay")
                .setEntity("entity")
                .setMapper("mapper")
                .setXml("mybatis.mapper")
                ;

        TemplateConfig templateConfig = new TemplateConfig()
                .setEntity("\\templates\\entity.zm.java");
//                .setEntity("C:\\Users\\z01140\\IdeaProjects\\demo\\demo-sc\\mybatisplus\\src\\main\\resources\\templates\\entity.zm.java");


        StrategyConfig strategyConfig =
                new StrategyConfig().setEntityTableFieldAnnotationEnable(!isDTO)
                        .setSkipView(true)
                        .setCapitalMode(true)
                        .setColumnNaming(NamingStrategy.underline_to_camel)
                        .setNaming(NamingStrategy.underline_to_camel)
                        .setEntityBooleanColumnRemoveIsPrefix(true)
                        .setEntityLombokModel(!isDTO)
                        .setInclude(tableName)
                        .setEntityColumnConstant(true)
                        .setTablePrefix("t_")
                ;

        new AutoGenerator().setDataSource(dataSourceConfig).setGlobalConfig(globalConfig).setPackageInfo(packageConfig).setTemplate(templateConfig).setStrategy(strategyConfig).execute();
    }
}
